import pygame

class Player(pygame.sprite.Sprite):
    ''' Player class represents the controlable avatar of the player in the game.
    It's responsible for tracking the player's speed and movement, and checking
    for collisions.
    You can call:
            change_speed(x,y)
            update(walls, firewall_mode)
    '''

    # Set speed vector
    change_x = 0
    change_y = 0

    # Constructor function
    def __init__(self, start_tile_coords, config):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        self.config = config

        self.change_x = 0
        self.change_y = 0

        # Set Player Size according to the difficulty set in the Config
        self.player_size = \
            self.config.difficulty_multiplier[self.config.difficulty]

        #Set the player to be a white square
        self.image = pygame.Surface([self.player_size, self.player_size])
        self.image.fill(config.colors['white'])

        # Set Player location
        # the coordinates are grid coordinates, not pixel coordinates, so we
        # have to calculate the pixels coords it's the coordinate times the tile
        # size -- that would put the player at the very edge of the tile take
        # that and add tile size divided by 2 -- that puts the left or top dead
        # center in the tile take that and subtract half the player size -- that
        # puts the center of the player exactly in the center of the tile
        self.rect = self.image.get_rect()
        self.rect.left = start_tile_coords[0] * self.config.tile_size + \
                         self.config.tile_size / 2 - self.player_size / 2
        self.rect.top = start_tile_coords[1] * self.config.tile_size + \
                        self.config.tile_size / 2 - self.player_size / 2

    # Change the speed of the player
    def change_speed(self, x, y):
        self.change_x += x
        self.change_y += y

    # Find a new position for the player
    def update(self, walls, firewall_mode):
        #In this function We update the x and y coordinates and check
        # for collisions separately because that allows for smoother motion

        # Get the old position, in case we need to go back to it
        old_x = self.rect.left
        self.rect.left = old_x + self.change_x

        # Did this update cause us to hit a wall?
        if pygame.sprite.spritecollide(self, walls, False):
            # Whoops, hit a wall. Go back to the old position
            self.rect.left = old_x
            # In firewall mode, hitting a wall kills the player, so return True
            if firewall_mode: return True

        old_y = self.rect.top
        self.rect.top = old_y + self.change_y

        # Did this update cause us to hit a wall?
        if pygame.sprite.spritecollide(self, walls, False):
            # Whoops, hit a wall. Go back to the old position
            self.rect.top = old_y
            # In firewall mode, hitting a wall kills the player, so return True
            if firewall_mode: return True
